/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pkl53.component;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import pkl53.validasi.Validator;

/**
 *
 * @author User
 */
public class Textfield extends JFormattedTextField implements Serializable{
    private JPanel panel;
    private int length;
    private char charType;
    private char minDigit;
    private char maxDigit;
    private boolean  valid;
    private Color color;
    private JTextField textfield;
    private boolean nextType;
    private Validator validator;
    private boolean required;
    
    public Textfield(){
       
        this.setText("1");
        setLength(1);
        this.setBackground(new Color(255, 255, 255));
        setFocusTraversalKeysEnabled(false);
        addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent evt){
                Component focused = evt.getComponent();
                focused.setBounds(focused.getBounds().x+10, focused.getBounds().y+50,focused.getBounds().width, focused.getBounds().height);
                getPanel().scrollRectToVisible(evt.getComponent().getBounds());
                repaint();
            }
        });
        
       

         
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //proses validasi dijalankan ketika tombol enter/tab/panah bawah/panah kanan ditekan.
                Textfield.this.setBackground(new Color(255, 255, 255));
                if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_DOWN
                        || e.getKeyCode() == KeyEvent.VK_TAB) {
                    //validator.onCheckValue(Textfield.this);cek validasi
                    System.out.println(getValidator());
                    if ("".equals(Textfield.this.getText().trim())) {
                        if (isRequired()) {
                            
                            Textfield.this.setBackground(Color.red);
                            JOptionPane.showMessageDialog(null,"Field must not be empty!");
                            Textfield.this.requestFocusInWindow();
                            
                        } else {//seting validator from propertises
                            if (getValidator() != null) {
                                boolean isValid = false;
                                isValid = true;
                                if (isValid) {
                                    Textfield.this.transferFocus();
                                } else {
                                    Textfield.this.setBackground(Color.red);
                                    Textfield.this.requestFocusInWindow();
                                }
                            } else {
                                Textfield.this.transferFocus();
                            }
                            
                        }
                    }else{Textfield.this.transferFocus();}
                }
                if(e.getKeyCode() == KeyEvent.VK_UP || (e.isShiftDown() && e.getKeyCode() == KeyEvent.VK_TAB)){
                Textfield.this.transferFocusBackward();
                }
            }
            @Override
            public void keyReleased(KeyEvent e){
                if(Textfield.this.getText().length()==getLength()){
                   // valid = validator.onCheckValue(Textfield.this);
                    if(isValid()){
                        if(isNextType()){
                               Textfield.this.setBackground(Color.blue);
                            getTextfield().grabFocus();
                             
                        }
                        else{
                             Textfield.this.setBackground(Color.blue);
                            Textfield.this.transferFocus();
                            
                        }
                    }
                    else{
                        Textfield.this.setBackground(Color.red);
                        Textfield.this.requestFocusInWindow();
                        
                    }
                }}
        }
        );
            
         setDocument(
                new PlainDocument() {
                    @Override
                    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                        StringBuffer buffer = new StringBuffer();
                        char[] cs = str.toCharArray();
                        int s = 0;
                        if (Textfield.this.getText().length() < length) {
                            for (int i = 0; i < cs.length; i++) {
                                //digit yang bisa diatur angka berapa aja yang boleh
                                //bisa untuk uang juga
                                if (charType == '1') {
                                    if (cs[i] >= minDigit && cs[i] <= maxDigit) {
                                        cs[s] = cs[i];
                                        s++;
                                    }
                                //just karakter
                                }else if(charType=='2'){
                                    if(Character.isLetter(cs[i])|| cs[i] == ' ' || cs[i] == ',' || cs[i] == '(' || cs[i] == ')' || cs[i] == '.'|| cs[i] == '-'|| cs[i] == '\''){
                                        cs[s] = Character.toLowerCase(cs[i]);
                                        s++;
                                    }
                                }
                                //untuk nim
                                else if (charType == '3') {
                                    if (Character.isDigit(cs[i])) {
                                        cs[s] = cs[i];
                                        s++;
                                    } else if (cs[i] == '.' && !Textfield.this.getText().contains(".")) {
                                        cs[s] = cs[i];
                                        s++;
                                    }
                                }
                                else{
                                    cs[s] = cs[i];
                                        s++;
                                
                                
                                }
                                                    
                            
                            }
                            buffer.append(cs, 0, s);
                          
                            }
                        super.insertString(offs, new String(buffer), a);
                    
                }
    
    });
    }


    /**
     * @return the panel
     */
    public JPanel getPanel() {
        return panel;
    }

    /**
     * @param panel the panel to set
     */
    public void setPanel(JPanel panel) {
        this.panel = panel;
    }

    /**
     * @return the length
     */
    public int getLength() {
        return length;
    }

    /**
     * @param length the length to set
     */
    public void setLength(int length) {
        this.length = length;
    }

    /**
     * @return the charType
     */
    public char getCharType() {
        return charType;
    }

    /**
     * @param charType the charType to set
     */
    public void setCharType(char charType) {
        this.charType = charType;
    }

    /**
     * @return the minDigit
     */
    public char getMinDigit() {
        return minDigit;
    }

    /**
     * @param minDigit the minDigit to set
     */
    public void setMinDigit(char minDigit) {
        this.minDigit = minDigit;
    }

    /**
     * @return the maxDigit
     */
    public char getMaxDigit() {
        return maxDigit;
    }

    /**
     * @param maxDigit the maxDigit to set
     */
    public void setMaxDigit(char maxDigit) {
        this.maxDigit = maxDigit;
    }

    /**
     * @return the valid
     */
    @Override
    public boolean isValid() {
        return valid;
    }

    /**
     * @param valid the valid to set
     */
    public void setValid(boolean valid) {
        this.valid = valid;
    }

    /**
     * @return the color
     */
    public Color getColor() {
        return color;
    }

    /**
     * @param color the color to set
     */
    public void setColor(Color color) {
        this.color = color;
    }

    /**
     * @return the textfield
     */
    public JTextField getTextfield() {
        return textfield;
    }

    /**
     * @param textfield the textfield to set
     */
    public void setTextfield(JTextField textfield) {
        this.textfield = textfield;
    }

    /**
     * @return the nextType
     */
    public boolean isNextType() {
        return nextType;
    }

    /**
     * @param nextType the nextType to set
     */
    public void setNextType(boolean nextType) {
        this.nextType = nextType;
    }

    /**
     * @return the validator
     */
    public Validator getValidator() {
        return validator;
    }

    /**
     * @param validator the validator to set
     */
    public void setValidator(Validator validator) {
        this.validator = validator;
    }

    /**
     * @return the required
     */
    public boolean isRequired() {
        return required;
    }

    /**
     * @param required the required to set
     */
    public void setRequired(boolean required) {
        this.required = required;
    }
}
